package com.erb.task;

import com.erb.entity.Redpacketrecord;
import com.erb.entity.User;
import com.erb.service.wzq.IPersonalService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by MoYao on 2020/6/22.
 * 定时任务
 */
@Component
public class RedpacketTimer {

	@Resource
	private IPersonalService ips;

	//红包定时
	@Scheduled(cron = "0/60 * * * * ?")  //cron不支持除数字、*、？外的字符
	public void redPacketTimer() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Redpacketrecord> list = ips.listredpa();
		for (Redpacketrecord red : list) {
			try {
				if (red.getState() == 1 && !red.getDeadline().equals("")) {
					//format：Date转String
					//parse：String转Date
					Date date = sdf.parse(red.getDeadline());
					long hh = new Date().getTime() - date.getTime();
					long ss = hh / (1000 * 60 * 60 * 24);
					if (ss >= 1) {
						red.setState(3);
						ips.updredp(red.getId(), red.getState());
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
	}

	//每月更新为用户名可修改
	@Scheduled(cron = "0 0 0 1 * ?")  //cron不支持除数字、*、？外的字符
	public void redRedpacketTimer2() {
		List<User> list = ips.listuser();
		for (User user : list) {
			if (user.getModificationTime() == 2) {
				user.setModificationTime(1);
				ips.updmod(user.getId(), user.getModificationTime());
			}
		}
	}
}
